package com.gitee.loyo.jdbc

import com.gitee.loyo.KSQL
import com.gitee.loyo.KSQL.formatter
import java.io.InputStream
import java.io.Reader
import java.lang.RuntimeException
import java.math.BigDecimal
import java.net.URL
import java.sql.*
import java.sql.Array
import java.sql.Date
import java.util.*
import kotlin.collections.ArrayList

class Stmt(private val stmt: Statement): Statement by stmt{
    private var closeListener: (() -> Unit)? = null

    fun setOnCloseListener(closeListener: () -> Unit): Stmt {
        this.closeListener = closeListener
        return this
    }

    override fun executeQuery(sql: String?): ResultSet {
        return stmt.executeQuery(sql).also {
            KSQL.log("<== {}", sql)
        }
    }


    override fun executeUpdate(sql: String, columnIndexes: IntArray?): Int {
        return stmt.executeUpdate(sql, columnIndexes).also {
            KSQL.log("<== {}", sql)
            KSQL.log("==> {}", it)
        }
    }

    override fun executeUpdate(sql: String, columnNames: kotlin.Array<out String>?): Int {
        return stmt.executeUpdate(sql, columnNames).also {
            KSQL.log("<== {}", sql)
            KSQL.log("==> {}", it)
        }
    }


    override fun executeUpdate(sql: String?): Int {
        return stmt.executeUpdate(sql).also {
            KSQL.log("<== {}", sql)
            KSQL.log("==> {}", it)
        }
    }

    override fun executeUpdate(sql: String?, autoGeneratedKeys: Int): Int {
        return stmt.executeUpdate(sql, autoGeneratedKeys).also {
            KSQL.log("<== {}", sql)
            KSQL.log("==> {}", it)
        }
    }

    override fun execute(sql: String?, columnNames: kotlin.Array<out String>?): Boolean {
        return stmt.execute(sql, columnNames)
    }

    override fun execute(sql: String?): Boolean {
        return stmt.execute(sql).also {
            KSQL.log("<== {}", sql)
            KSQL.log("==> {}", it)
        }
    }

    override fun execute(sql: String?, columnIndexes: IntArray?): Boolean {
        return stmt.execute(sql, columnIndexes).also {
            KSQL.log("<== {}", sql)
            KSQL.log("==> {}", it)
        }
    }

    override fun execute(sql: String?, autoGeneratedKeys: Int): Boolean {
        return stmt.execute(sql, autoGeneratedKeys).also {
            KSQL.log("<== {}", sql)
            KSQL.log("==> {}", it)
        }
    }

    override fun executeBatch(): IntArray {
        return stmt.executeBatch().also {
            KSQL.log("==> {}", it)
        }
    }

    override fun executeLargeBatch(): LongArray {
        return stmt.executeLargeBatch().also {
            KSQL.log("==> {}", it)
        }
    }

    override fun executeLargeUpdate(sql: String): Long {
        return stmt.executeLargeUpdate(sql).also {
            KSQL.log("<== {}", sql)
            KSQL.log("==> {}", it)
        }
    }

    override fun executeLargeUpdate(sql: String, autoGeneratedKeys: Int): Long {
        return stmt.executeLargeUpdate(sql, autoGeneratedKeys).also {
            KSQL.log("<== {}", sql)
            KSQL.log("==> {}", it)
        }
    }

    override fun executeLargeUpdate(sql: String, columnIndexes: IntArray?): Long {
        return stmt.executeLargeUpdate(sql, columnIndexes).also {
            KSQL.log("<== {}", sql)
            KSQL.log("==> {}", it)
        }
    }

    override fun executeLargeUpdate(sql: String, columnNames: kotlin.Array<out String>?): Long {
        return stmt.executeLargeUpdate(sql, columnNames).also {
            KSQL.log("<== {}", sql)
            KSQL.log("==> {}", it)
        }
    }


    override fun close() {
        stmt.close()
        closeListener?.invoke()
    }
}